/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.applet.internal;

import org.kad.gwt.web.client.applet.AppletListener;

import netscape.javascript.JSObject;

/**
 * Listener adapter to allow call from gwt method.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
public class AppletListenerJSAdapter implements AppletListener {
    /**
     * The target listener
     */
    private final JSObject listener;

    /**
     * Constructor.
     *
     * @param listenerIn {@link #listener}
     */
    public AppletListenerJSAdapter(final JSObject listenerIn) {
        super();
        this.listener = listenerIn;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void handle(Object param1, Object param2, Object param3) {
        if (param3 != null) {
            listener.call("setCallback", new Object[] {param3});
        }
        listener.call("handle", new Object[] {param1, param2, null});
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((listener == null) ? 0 : listener.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        AppletListenerJSAdapter other = (AppletListenerJSAdapter) obj;
        if (listener == null) {
            if (other.listener != null)
                return false;
        } else if (!listener.equals(other.listener))
            return false;
        return true;
    }
    
}
